package com.yic.module.device.controller.admin.inspectionPlanJob;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yic.module.device.controller.admin.inspectionPlan.vo.InspectionPlanExportReqVO;
import com.yic.module.device.dal.dataobject.inspectionPlan.InspectionPlanDO;
import com.yic.module.device.service.inspectionPlan.InspectionPlanService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.device.controller.admin.inspectionPlanJob.vo.*;
import com.yic.module.device.dal.dataobject.inspectionPlanJob.InspectionPlanJobDO;
import com.yic.module.device.convert.inspectionPlanJob.InspectionPlanJobConvert;
import com.yic.module.device.service.inspectionPlanJob.InspectionPlanJobService;

@Tag(name = "管理后台 - 巡检计划与任务关联")
@RestController
@RequestMapping("/device/inspection-plan-job")
@Validated
public class InspectionPlanJobController {

    @Resource
    private InspectionPlanJobService inspectionPlanJobService;
    @Resource
    private InspectionPlanService inspectionPlanService;

    @PostMapping("/create")
    @Operation(summary = "创建巡检计划与任务关联")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-job:create')")
    public CommonResult<Long> createInspectionPlanJob(@Valid @RequestBody InspectionPlanJobCreateReqVO createReqVO) {
        return success(inspectionPlanJobService.createInspectionPlanJob(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新巡检计划与任务关联")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-job:update')")
    public CommonResult<Boolean> updateInspectionPlanJob(@Valid @RequestBody InspectionPlanJobUpdateReqVO updateReqVO) {
        inspectionPlanJobService.updateInspectionPlanJob(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除巡检计划与任务关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-job:delete')")
    public CommonResult<Boolean> deleteInspectionPlanJob(@RequestParam("id") Long id) {
        inspectionPlanJobService.deleteInspectionPlanJob(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得巡检计划与任务关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-job:query')")
    public CommonResult<InspectionPlanJobRespVO> getInspectionPlanJob(@RequestParam("id") Long id) {
        InspectionPlanJobDO inspectionPlanJob = inspectionPlanJobService.getInspectionPlanJob(id);
        return success(InspectionPlanJobConvert.INSTANCE.convert(inspectionPlanJob));
    }

    @GetMapping("/list")
    @Operation(summary = "获得巡检计划与任务关联列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-job:query')")
    public CommonResult<List<InspectionPlanJobRespVO>> getInspectionPlanJobList(@RequestParam("ids") Collection<Long> ids) {
        List<InspectionPlanJobDO> list = inspectionPlanJobService.getInspectionPlanJobList(ids);
        return success(InspectionPlanJobConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得巡检计划与任务关联分页")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-job:query')")
    public CommonResult<PageResult<InspectionPlanJobRespVO>> getInspectionPlanJobPage(@Valid InspectionPlanJobPageReqVO pageVO) {
        if (ObjectUtil.isNotNull(pageVO.getType())) {
            List<String> planIdList = new ArrayList<>();
            InspectionPlanExportReqVO vo = new InspectionPlanExportReqVO();
            vo.setType(pageVO.getType());
            List<InspectionPlanDO> inspectionPlanList = inspectionPlanService.getInspectionPlanList(vo);
            if (CollectionUtil.isNotEmpty(inspectionPlanList)){
                planIdList = inspectionPlanList.stream().map(item->item.getId()+"").collect(Collectors.toList());
            } else {
                planIdList.add("-1");
            }
            pageVO.setPlanIdList(planIdList);
        }
        PageResult<InspectionPlanJobDO> pageResult = inspectionPlanJobService.getInspectionPlanJobPage(pageVO);
        PageResult<InspectionPlanJobRespVO> result = InspectionPlanJobConvert.INSTANCE.convertPage(pageResult);
        result.getList().forEach(item -> {
            InspectionPlanDO inspectionPlan = inspectionPlanService.getInspectionPlan(item.getPlanId());
            item.setName(inspectionPlan.getName());
            item.setStartDate(inspectionPlan.getStartDate());
            item.setEndDate(inspectionPlan.getEndDate());
            item.setCycle(inspectionPlan.getCycle());
            item.setReminderTime(inspectionPlan.getReminderTime());
            item.setEstimatedTime(inspectionPlan.getEstimatedTime());
        });
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出巡检计划与任务关联 Excel")
    @PreAuthorize("@ss.hasPermission('device:inspection-plan-job:export')")
    @OperateLog(type = EXPORT)
    public void exportInspectionPlanJobExcel(@Valid InspectionPlanJobExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<InspectionPlanJobDO> list = inspectionPlanJobService.getInspectionPlanJobList(exportReqVO);
        // 导出 Excel
        List<InspectionPlanJobExcelVO> datas = InspectionPlanJobConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "巡检计划与任务关联.xls", "数据", InspectionPlanJobExcelVO.class, datas);
    }

}
